package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.Berth;
import com.example.elderlycare.dao.mapper.BerthMapper;
import com.example.elderlycare.service.BerthService;
import com.example.elderlycare.service.entity.BerthItem;
import com.example.elderlycare.service.entity.BerthListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BerthServiceImpl extends ServiceImpl<BerthMapper, Berth> implements BerthService {
    @Resource
    BerthMapper berthMapper;

    @Override
    public BerthListResponseBody queryBerths(Map<String, Object> params) {
        List<BerthItem> berthItems = new ArrayList<>();
        int total = berthMapper.getBerthCount(params);
        List<Berth> berths = berthMapper.queryBerths(params);
        berths.forEach(item -> {
            BerthItem berthItem = BerthItem.builder()
                    .id(item.getId())
                    .elderName(item.getElderName() != null ? item.getElderName() : "该床位无老人")
                    .free(item.getElderId() != null ? 0 : 1)
                    .number(item.getNumber())
                    .nursingHome(item.getNursingHome())
                    .build();
            berthItems.add(berthItem);
        });
        return BerthListResponseBody.builder()
                .total(total)
                .berthItemList(berthItems)
                .build();
    }


}
